package com.feather.common.data.simple;

import com.feather.common.data.Column;
import com.feather.common.data.DataColumn;
import com.feather.common.data.DataItemType;

/**
 * @author flogyin
 */
public class SimpleDataColumn implements DataColumn, java.io.Serializable {
	private static final long	serialVersionUID	= 1L;

	protected Column			column;
	protected Object			value;

	public SimpleDataColumn(Column column) {
		this.column = column;
	}

	@Override
	public DataItemType getItemType() {
		return DataItemType.DataColumnItem;
	}

	@Override
	public Column getColumn() {
		return this.column;
	}

	@Override
	public boolean asBoolean() {
		if (this.value != null) {
			if (this.value instanceof Boolean) {
				return (Boolean) this.value;
			}
			String temp = this.value.toString();
			return "true".equalsIgnoreCase(temp) || "1".equals(temp);
		}
		return false;
	}

	@Override
	public boolean getBoolean() {
		return (Boolean) this.value;
	}

	@Override
	public void setBoolean(boolean value) {
		this.value = value;
	}

	@Override
	public String asString() {
		return this.value != null ? (String) this.value : "";
	}

	@Override
	public String getString() {
		return (String) this.value;
	}

	@Override
	public void setString(String value) {
		this.value = value;
	}

	@Override
	public byte asByte() {
		if (this.value != null) {
			if (this.value instanceof Byte) {
				return (Byte) this.value;
			}
			String temp = this.value.toString();
			return Byte.parseByte(temp);
		}
		return 0;
	}

	@Override
	public byte getByte() {
		return (Byte) this.value;
	}

	@Override
	public void setByte(byte value) {
		this.value = value;
	}

	@Override
	public byte[] getBytes() {
		return (byte[]) this.value;
	}

	@Override
	public void setBytes(byte[] value) {
		this.value = value;
	}

	@Override
	public short asShort() {
		if (this.value != null) {
			if (this.value instanceof Short) {
				return (Short) this.value;
			}
			String temp = this.value.toString();
			return Short.parseShort(temp);
		}
		return 0;
	}

	@Override
	public short getShort() {
		return (Short) this.value;
	}

	@Override
	public void setShort(short value) {
		this.value = value;
	}

	@Override
	public int asInt() {
		if (this.value != null) {
			if (this.value instanceof Integer) {
				return (Integer) this.value;
			}
			String temp = this.value.toString();
			return Integer.parseInt(temp);
		}
		return 0;
	}

	@Override
	public int getInt() {
		return (Integer) this.value;
	}

	@Override
	public void setInt(int value) {
		this.value = value;
	}

	@Override
	public float asFloat() {
		if (this.value != null) {
			if (this.value instanceof Float) {
				return (Float) this.value;
			}
			String temp = this.value.toString();
			return Float.parseFloat(temp);
		}
		return 0F;
	}

	@Override
	public float getFloat() {
		return (Float) this.value;
	}

	@Override
	public void setFloat(float value) {
		this.value = value;
	}

	@Override
	public long asLong() {
		if (this.value != null) {
			if (this.value instanceof Long) {
				return (Long) this.value;
			}
			String temp = this.value.toString();
			return Long.parseLong(temp);
		}
		return 0L;
	}

	@Override
	public long getLong() {
		return (Long) this.value;
	}

	@Override
	public void setLong(long value) {
		this.value = value;
	}

	@Override
	public double asDouble() {
		if (this.value != null) {
			if (this.value instanceof Double) {
				return (Double) this.value;
			}
			String temp = this.value.toString();
			return Double.parseDouble(temp);
		}
		return 0D;
	}

	@Override
	public double getDouble() {
		return (Double) this.value;
	}

	@Override
	public void setDouble(double value) {
		this.value = value;
	}

	@Override
	public Object getObject() {
		return this.value;
	}

	@Override
	public void setObject(Object value) {
		this.value = value;
	}
}
